<?php

// 审核会员
// PUT member.admin.audit

namespace app\api\resource\member\admin;
use think\Db;

class Audit
{
    // 方法
    public $_method = ['PUT'];
    // 前置
    public $_pre    = ['adminLogin'];
    // 描述
    public $_description = '审核会员';
    // 参数
    public $_param  = [
        'member_id' => '会员主键',
        'status'    => '审核状态,0-正常 1-锁定',
    ];
    // 返回
    public $_return = [
        201 => '操作成功',
        404 => '帐号不存在',
        422 => '会员主键必须|审核状态必须',
        500 => '服务器异常',
    ];

    public function run(&$request)
    {
        $memberId = $request->put('member_id/d',0);
        $status   = $request->put('status/d',-1);

        if (0 == $memberId) return [422,'会员主键必须'];
        if (-1 == $status)  return [422,'审核状态必须'];

        $ret = Db::name('member')->where('id',$memberId)->find();
        if ( is_null($ret) ) return [404,'帐号不存在'];

        $data['status'] = $status;
        $num = Db::name('member')->where('id',$memberId)->update($data);
        if ($num > 0) {
            if ($ret['role'] > 1) {
                // 通过投资喜好认证
                $state = $status ? 0 : 1;
                Db::name('member_like')->where('member_id',$memberId)->update(['status'=>$state]);
                // 同步通过投资机构
                if (0 == $status && 3 == $ret['role']) {
                    Db::name('organization')->where('title',$ret['organization'])->update(['status'=>0]);
                }
            }
            return [ 201,[] ];
        } else {
            return 500;
        }
    }
}
